package com.cml.dm.engine.config;

import com.cml.dm.engine.model.Node;
import org.dom4j.Element;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;

public class NodeXmlNodeParser <T> implements  XmlNodeParser{
    public void parser(List list, Element el) {
        Node  node = new Node();
        node.setId(el.attributeValue("id"));
        node.setTable(el.attributeValue("table"));
        node.setPk(el.attributeValue("pk"));
        node.setNext(el.attributeValue("next"));
        node.setJoinUpPkNode(el.attributeValue("joinUpPkNode"));
        node.setInterceptor(el.attributeValue("interceptor"));
        node.setSortField(el.attributeValue("sortField"));
        node.setJoinNodeId(el.attributeValue("joinNodeId"));
        node.setShardingColumn(el.attributeValue("shardingColumn"));
        if(null != el.attributeValue("limit")){
            node.setLimit(Integer.parseInt((String)el.attributeValue("limit")));
        }
        node.setOrgId(Optional.ofNullable(el.attributeValue("orgId")).orElse("org_id"));
        for(Iterator i = el.elementIterator(); i.hasNext();){
            Element elTemp = (Element) i.next();
            if("backupSql".equals(elTemp.getName())){
                node.setBackupSql(elTemp.getText());
            }else if("recoverySql".equals(elTemp.getName())){
                node.setRecoverySql(elTemp.getText());
            }
        }
        list.add(node);
    }
}
